var statusDatas = com.selectDictDatas("sys_normal_disable")

$(function () {
    $("#jqGrid").jqGrid({
        url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + 'sys/dicttype/list',
        datatype: "json",
        colModel: [
            {label: '主键', name: 'id', index: 'id', width: 50, key: true},
            {label: '字典名称', name: 'name', index: 'name', width: 80}
            , {
                label: '字典类型', name: 'type', index: 'type', width: 80,
                formatter: function (value, options, row) {
                    return '<a href="#" onclick="window.open(\''+(window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") +"modules/sys/dictdata.html?type="+value+'\')">'+value+'</a>'
                }
            }
            , {
                label: '状态（0正常 1停用）', name: 'status', index: 'status', width: 80,
                formatter: function (value, options, row) {
                    let dictLabel = com.selectDictLabel(statusDatas, value);
                    return dictLabel
                }
            }
            , {label: '备注', name: 'remark', index: 'remark', width: 80}
            ,],
        viewrecords: true,
        height: "100%",
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#myapp',
    data: {
        showList: true,
        title: null,
        params: {
            name: null
        },
        dictType: {},
        statusDatas: []
    },
    created: function () {
        this.statusDatas = statusDatas
    },
    methods: {
        query: function () {
            //初始化查询分页为1，默认从第一页查
            $("#jqGrid").jqGrid('setGridParam', {
                //添加查询条件
                postData: {},
                page: 1
            });
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.dictType = {
                status: 0
            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.dictType.id == null ? "sys/dicttype/save" : "sys/dicttype/update";

            //请求参数
            var param = JSON.stringify(vm.dictType);

            com.AjaxPost((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + url, param, "json", function (r) {
                if (r.success) {
                    toast('操作成功');
                    vm.reload();
                } else {
                    warning(r.msg);
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                var url = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/dicttype/delete";
                //请求参数
                var param = JSON.stringify(ids);
                com.AjaxPost(url, param, "json", function (r) {
                    if (r.success) {
                        toast('操作成功');
                        vm.reload();
                    } else {
                        warning(r.msg);
                    }
                });
            });
        }, openImportExcel: function (event) {
            openImportExcel();
        },
        importExcel: function (event) {
            let prefix = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/dicttype";
            importExcel(prefix, null, vm);
        }, exportExcelTemplate: function (event) {
            let prefix = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/dicttype";
            exportExcelTemplate(prefix);
        },
        exportExcel: function (event) {
            var prefix = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/dicttype";
            exportExcel(prefix);
        },
        getInfo: function (id) {
            var url = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/dicttype/info/" + id;
            com.AjaxGet(url, null, "json", function (r) {
                vm.dictType = r.entity;
            }, null, false)
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                //添加查询条件
                postData: {
                    name: vm.params.name
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});